/**
 * 
 */
package com.manage.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.action.AbstractAction;
import com.config.Config;
import com.manage.cache.ChannelManager;
import com.manage.cache.CpAppChannelManager;
import com.manage.cache.CpAppInfoManager;
import com.manage.cache.CpInfoManager;
import com.manage.cache.ProjectManager;
import com.manage.model.Channel;
import com.manage.model.CpAppChannel;
import com.manage.model.CpAppInfo;
import com.manage.model.CpInfo;
import com.manage.model.Project;
import com.util.ConfigUtil;
import com.util.FileDigest;
import com.util.Util;

/**
 * @author jinmei.wang
 *
 */
public class CpAppInfoAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger;
	
	public CpAppInfoAction()
	{
		logger = Logger.getLogger(this.getClass());
	}
	
	public String execute()
	{
		return SUCCESS;
	}

	private List<CpAppInfo> list;
	private CpAppInfo       model;
	private String        type;
	private CpInfo cp;
	private File appicon;
	private String appiconFileName;
	private String appiconContentType;
	private File appfile;
	private String appfileFileName;
	private String appfileContentType;
	private int channelid;
	private int projectid ;
	
	public String list()
	{
		channels = ChannelManager.getInstance().getChannels();
		logger.debug("list account");
		cp = CpInfoManager.getInstance().getCpInfo(cp.getId());
		list = CpAppInfoManager.getInstance().getCpAppInfos(cp.getId());
		return SUCCESS;
	}
	
	public String show()
	{
		/*channels = ChannelManager.getInstance().getChannels();
		if (model.getId() >= 0)
		{
			model = CpAppInfoManager.getInstance().getCpAppInfo(model.getId());
			type = "update";
			projects = ProjectManager.getInstance().getProjects(model.getChannelid());
		}
		else
		{
			model.setId(-1);
			type = "insert";
		}*/
		return SUCCESS;
	}
	
	public String save()
	{
		/*if(type.equals("offline") || type.equals("online"))
		{
			
		}else
		{
			model.setChannelid(channelid);
			model.setProjectid(projectid);
			if (type.equals("insert"))
			{
				model.setAppnumber(CpAppInfoManager.getInstance().genAppNumber());
				model.setCreatedate(Util.dateToString(new Date()));
				model.setCreateuser(operator.getName());
			}
			//修改时将文件信息放入model
			if(type.equals("update"))
			{
				CpAppInfo m1 = CpAppInfoManager.getInstance().getCpAppInfo(model.getId());
				model.setAppfilename(m1.getAppfilename());
				model.setAppfilesrc(m1.getAppfilesrc());
				model.setRefilename(m1.getRefilename());
				model.setAppiconname(m1.getAppiconname());
				model.setAppiconsrc(m1.getAppiconsrc());
				model.setReiconname(m1.getReiconname());
				model.setAppmd5(m1.getAppmd5());
			}
			if (appicon != null)
			{
				model.setAppiconname(appiconFileName);
				String reiconname = FileDigest.getFileMD5(appicon);
				String appiconsrc = ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_CP_APP_SAVE_PATH) + 
				"/"+reiconname;
				saveUploadFile(appicon,appiconsrc);
				model.setAppiconsrc(appiconsrc);
				model.setReiconname(reiconname);
			}
			String filemd5 = "";
			if (appfile != null)
			{
				model.setAppfilename(appfileFileName);
				filemd5 = FileDigest.getFileMD5(appfile);
				String appfilesrc = ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_CP_APP_SAVE_PATH) + 
				"/"+filemd5;
				saveUploadFile(appfile,appfilesrc);
				model.setAppfilesrc(appfilesrc);
				model.setRefilename(filemd5);
				model.setAppmd5(filemd5);
			}
		}
		CpAppInfoManager.getInstance().saveCpAppInfo(operator, type, model);
		
		if(type.equals("offline") || type.equals("online"))
			model = CpAppInfoManager.getInstance().getCpAppInfo(model.getId());*/
		return SUCCESS;
	}
	
	private List<Channel> channels;
	private List<Project> projects;
	private CpAppChannel cpappc;
	//添加cp-app的绑定
	public String showproject()
	{
		/*model = CpAppInfoManager.getInstance().getCpAppInfo(model.getId());
		channels = ChannelManager.getInstance().getChannels();
		cpappc = CpAppChannelManager.getInstance().getCpAppChannel(model.getAppnumber());
		if(cpappc!=null)
		{
			type = "update";
		}else
		{
			type = "insert";
		}*/
		return SUCCESS;
	}
	
	
	
	public int getChannelid() {
		return channelid;
	}

	public void setChannelid(int channelid) {
		this.channelid = channelid;
	}

	public int getProjectid() {
		return projectid;
	}

	public void setProjectid(int projectid) {
		this.projectid = projectid;
	}

	public String changechannel()
	{
		projects = ProjectManager.getInstance().getProjects(channelid);
		return SUCCESS;
	}

	
	public List<Channel> getChannels() {
		return channels;
	}

	public void setChannels(List<Channel> channels) {
		this.channels = channels;
	}

	public List<Project> getProjects() {
		return projects;
	}

	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}

	public CpAppChannel getCpappc() {
		return cpappc;
	}

	public void setCpappc(CpAppChannel cpappc) {
		this.cpappc = cpappc;
	}

	public String getAppiconFileName() {
		return appiconFileName;
	}

	public void setAppiconFileName(String appiconFileName) {
		this.appiconFileName = appiconFileName;
	}

	public String getAppiconContentType() {
		return appiconContentType;
	}

	public void setAppiconContentType(String appiconContentType) {
		this.appiconContentType = appiconContentType;
	}

	public String getAppfileFileName() {
		return appfileFileName;
	}

	public void setAppfileFileName(String appfileFileName) {
		this.appfileFileName = appfileFileName;
	}

	public String getAppfileContentType() {
		return appfileContentType;
	}

	public void setAppfileContentType(String appfileContentType) {
		this.appfileContentType = appfileContentType;
	}

	public List<CpAppInfo> getList() {
		return list;
	}

	public void setList(List<CpAppInfo> list) {
		this.list = list;
	}

	public CpAppInfo getModel() {
		return model;
	}

	public void setModel(CpAppInfo model) {
		this.model = model;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public CpInfo getCp() {
		return cp;
	}

	public void setCp(CpInfo cp) {
		this.cp = cp;
	}

	public File getAppicon() {
		return appicon;
	}

	public void setAppicon(File appicon) {
		this.appicon = appicon;
	}

	public File getAppfile() {
		return appfile;
	}

	public void setAppfile(File appfile) {
		this.appfile = appfile;
	}
	
	

}
